//
//  BaseViewController.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/1.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class BaseViewController: UIViewController {
    
    var hud : NetProgressHudView?
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    var naviHide = false{
        didSet{
            self.navigationController?.isNavigationBarHidden = naviHide
        }
    }
    
    func commonAlertMsg(content : String) -> Void {
        alertSomeMessage(title: YXAlertTitle, content: content, sureTitle: YXConfirmTitle, cancelTitle: nil, ensureAction: {(alert) in
            
        }, cancelAction: {(alert) in
            
        })
    }
    
    //判断当前控制器是否正在显示
    func isCurrentViewController() -> Bool {
        if self.view.window != nil {
            if self.isViewLoaded == true {
                return true
            }
        }
        return false
    }
    
    func beginAnim() -> Void {
        if Thread.current.isMainThread {
            if let navView =  self.navigationController?.view {
                self.hud = NetProgressHudView.init(supView: navView, type: .Black)
                self.hud?.beginRotate()
                return
            }
        }
        DispatchQueue.main.async {
            if let navView = self.navigationController?.view {
                self.hud = NetProgressHudView.init(supView: navView, type: .Black)
                self.hud?.beginRotate()
            }
        }
    }
    
    //需要有NetProgressHud的支持
    func endAnim() -> Void {
        if Thread.current.isMainThread {
            self.hud?.endRotate()
            hud = nil
            return
        }
        DispatchQueue.main.async {
            self.hud?.endRotate()
            self.hud = nil
        }
    }
    
    func pops()->Void{
        let _ = self.navigationController?.popToRootViewController(animated: true)
    }
    
    internal func actionSheet(first : String?,second : String?,firstHandler : (()->Void)?,secondHandler : (()->Void)?)->Void{
        let alertConCtl = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
        if first != nil {
            if first!.count > 0 {
                let firstSheet = UIAlertAction.init(title: first, style: .default, handler: {(alert) in
                    firstHandler!()
                })
                alertConCtl.addAction(firstSheet)
            }
        }
        if second != nil {
            if second!.characters.count > 0 {
                let secondSheet = UIAlertAction.init(title: second, style: .default, handler: {(alert) in
                    secondHandler!()
                })
                alertConCtl.addAction(secondSheet)
            }
        }
        let cancel = UIAlertAction.init(title: "取消", style: .cancel, handler: {(alert) in
            
        })
        alertConCtl.addAction(cancel)
        self.present(alertConCtl, animated: true, completion: nil)
    }
    
    internal func alertSomeMessage(title: String?, content: String?, sureTitle: String?, cancelTitle: String?, ensureAction: @escaping (UIAlertAction) -> Void, cancelAction: @escaping (UIAlertAction) -> Void) {
        if isCurrentViewController() == true{
            let alertConCtl = UIAlertController.init(title: title, message: content, preferredStyle: UIAlertControllerStyle.alert)
            let ensureAction = UIAlertAction.init(title: sureTitle, style: UIAlertActionStyle.default, handler: ensureAction)
            if(cancelTitle != nil){
                let cancelAction = UIAlertAction.init(title: cancelTitle, style: UIAlertActionStyle.cancel, handler: cancelAction)
                alertConCtl.addAction(cancelAction)
            }
            alertConCtl.addAction(ensureAction)
            self.present(alertConCtl, animated: true) {
            }
        }
    }
    
    func addLine() -> Void {
        let bar = self.navigationController?.navigationBar
        let line = UIView.init()
        line.frame = CGRect.init(x: 0, y: 44, width: YXDeviceWidth, height: YXHeightScale)
        line.backgroundColor = UIColor.white
        bar?.addSubview(line)
    }
    
    func rightButton(title : String,target : AnyObject, selector : Selector) -> Void {
        let size = title.StringSize(font: UIFont.systemFont(ofSize: 30 * YXWidthScale))
        let itm = UIView.init(frame: CGRect.init(x: 0, y: 0, width: size.width, height: 44))
        let button = UIButton.init(type: .system)
        button.frame = itm.bounds
        button.titleLabel?.font = UIFont.systemFont(ofSize: 30 * YXWidthScale)
        button.setTitleColor(UIColor.black, for: .normal)
        button.setTitle(title, for: .normal)
        button.addTarget(target, action: selector, for: .touchUpInside)
        itm.addSubview(button)
        let item = UIBarButtonItem.init(customView: itm)
        self.navigationItem.rightBarButtonItem = item
    }
    
    func defaultBackButton() -> Void {
        self.navigationItem.hidesBackButton = true
        let itm = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 44))
        let imageView = UIImageView.init(frame: CGRect.init(x: 0, y: 14, width: 9, height: 16))
        
        imageView.image = UIImage.load(name: "nav_return")
        imageView.contentMode = .scaleAspectFill
        itm.addSubview(imageView)
        let size = "返回".StringSize(font: UIFont.systemFont(ofSize: 32 * YXWidthScale))
        let label = UILabel.init(frame: CGRect.init(x: imageView.right() + 12 * YXWidthScale, y: imageView.top() - (size.height - imageView.height()) / 2, width: size.width, height: size.height))
        label.font = UIFont.systemFont(ofSize: 32 * YXWidthScale)
        label.textColor = UIColor.black
        label.textAlignment = .center
        label.text = "返回"
        itm.addSubview(label)
        let button = UIButton.init(type: .custom)
        button.frame = itm.bounds
        button.addTarget(self, action: #selector(pop), for: .touchUpInside)
        itm.addSubview(button)
        let item = UIBarButtonItem.init(customView: itm)
        self.navigationItem.leftBarButtonItem = item
    }
    
    @objc func pop() -> Void {
        let _ = self.navigationController?.popViewController(animated: true)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.isTranslucent = false
        self.tabBarController?.tabBar.isTranslucent = true
        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = naviHide
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }

}
